package com.liik.game.util;

import android.graphics.Canvas;
import android.graphics.Paint;

public class FPSCounter {
	public Paint paint;
	public int currentFPS = 0;
	public int fps = 0;
	public long start = 0;
	public final int ANALYZETIME = 2000;
	public boolean slowdown = false;
	
	public FPSCounter() {
		paint = new Paint();
		paint.setARGB(255, 255, 255, 255);
	}
	
	public void update(Canvas canvas) {
		tick();
		render(canvas);
	}

	private void tick() {
		currentFPS++;
		if (System.currentTimeMillis() - start >= ANALYZETIME) {
			fps = currentFPS / (ANALYZETIME / 1000);
			currentFPS = 0;
			start = System.currentTimeMillis();
			slowdown = false;
		}
	}

	private void render(Canvas canvas) {
		if (canvas != null) {
			canvas.drawText(fps + "fps" + (slowdown ? "!!" : ""), 10, 10, paint);
		}
	}
}